<template>
	<div class="table-demo-container layout-padding">
		<div class="table-demo-padding layout-padding-view layout-padding-auto">
			<!-- <TableSearch :search="state.tableData.search" @search="onSearch" @addRow="onTableEditRow" /> -->
			<div style="display: flex;height: 100%;">
				<div class="menu-left">
					<fast-tree-component :treeData="treeState.treeData" :treeProps="treeProps"
						@selectedCode="selectedCode"></fast-tree-component>
				</div>

				<div style="display: flex;flex-direction: column;flex: 1;">

					<div class="head" :style="headComputed" v-loading="treeState.treeFloorLoading">
						<div class="title" style="font-size: 18px;"> <span style="font-weight: 900;">区域名称：</span> {{
							treeState.treeName }}</div>
						<div style="display: flex;flex-wrap: wrap;margin-top: 20px;margin-left: 20px;"
							v-if="treeState.treeDevList?.length > 0">
							<div style="margin-right: 10px;" class="devItem" v-for="item in treeState.treeDevList">
								<el-tooltip placement="bottom">
									<template #content> 设备名称：{{ item.devName }}<br />设备编号：{{ item.devCode }} <br />在线状态：{{
										item.online === 1 ? '在线' : '离线' }} </template>
									<div v-if="item.openDoor === 1"><img :src="openImg" alt=""></div>
									<div v-else><img :src="closeImg" alt=""></div>
								</el-tooltip>
							</div>
						</div>
						<div v-else style="font-size: 16px;">暂无设备</div>
					</div>
					<div style="font-size: 18px;font-weight: 900;padding: 5px 10px;">异常事件：</div>
					<div style="width: 100%;overflow-y: auto;height: 55%;">
						<!-- <Table style="width: 100%;" ref="tableRef" v-bind="state.tableData" class="table-demo"
							@delRow="onTableDelRow" @editRow="onTableEditRow" @pageChange="onTablePageChange"
							@sortHeader="onSortHeader" /> -->
						<el-table :data="state.tableData.data" style="height: 100%;" border>
							<el-table-column prop="devCode" label="设备编号" />
							<el-table-column prop="devName" label="设备名称" />
							<el-table-column prop="employeeCode" label="工号" />
							<el-table-column prop="employeeName" label="姓名" />
							<el-table-column prop="eventTime" label="异常时间" />
						</el-table>
					</div>
				</div>
			</div>
			<editComponent ref="dialogRef" @refresh="getTableData()" />
		</div>
	</div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, reactive, ref, onMounted, onUnmounted } from 'vue';
import { useAccessPointByAreaQueryApi } from '/@/api/access/point';
import { RefType } from '/@/types/global';
import { useAccessAreaQueryApi, useAccessAreaTreeApi } from '/@/api/access/area';
import { computed } from 'vue';
import { useAccessAlarmByAreaPageApi } from '/@/api/access/alarm';
import openImg from '/@/assets/open.png'
import closeImg from '/@/assets/close.png'

const dialogRef = ref<RefType>();
const editComponent = defineAsyncComponent(() => import('./editDialog.vue'));
// 引入组件
const Table = defineAsyncComponent(() => import('/@/components/table/index.vue'));
const TableSearch = defineAsyncComponent(() => import('/@/components/table/noAddSearch.vue'));
const fastTreeComponent = defineAsyncComponent(() => import('/@/views/access/mapping/fastTree/index.vue'));

const treeProps = {
	label: 'name',
	key: 'code',
	children: 'children'
}

const headComputed = computed(() => {
	return {
		height: '45%',
		border: '1px solid #EBEEF5',
		background: `url(data:image/jpg;base64,${treeState.images})`,
		backgroundSize: '100% 100%',
		padding: '10px',
		boxSizing: 'border-box',
	}
}) as any

// 定义变量内容
const tableRef = ref<RefType>();
const state = reactive<TableDemoState>({
	tableData: {
		// 列表数据（必传）
		data: [],
		// 表头内容（必传，注意格式
		header: [
			{ key: 'alias', colWidth: '', title: '门名称', type: 'text', isCheck: true, sortable: true },
			{ key: 'deviceCode', colWidth: '', title: '设备序列号', type: 'text', isCheck: true, sortable: true },
			{ key: 'deviceName', colWidth: '', title: '设备名称', type: 'text', isCheck: true, sortable: true },
			{ key: 'inout', colWidth: '', title: '进出方向', type: 'text', isCheck: true, sortable: true },
			{ key: 'areaName', colWidth: '', title: '设备区域', type: 'text', isCheck: true, sortable: true },
		],
		// 配置项（必传）
		config: {
			total: 0, // 列表总数
			loading: true, // loading 加载
			isBorder: true, // 是否显示表格边框
			isSerialNo: false, // 是否显示表格序号
			isSelection: false, // 是否显示表格多选
			isOperate: true, // 是否显示表格操作栏
		},
		// 搜索表单，动态生成（传空数组时，将不显示搜索，注意格式）
		search: [
			{ label: '门名称', prop: 'alias', placeholder: '请输入门名称', required: false, type: 'input', defaultValue: '' },
			// { label: '设备序列号', prop: 'deviceCode', placeholder: '请输入设备序列号', required: false, type: 'input', defaultValue: '' }
		],
		// 搜索参数（不用传，用于分页、搜索时传给后台的值，`getTableData` 中使用）
		param: {
			areaCode: '',
			pageNum: 1,
			pageSize: 10,
		},
		// 打印标题
		printName: '打印示例',

	},
});
const treeState = reactive({
	treeData: [] as any,
	images: '' as any,
	treeName: '',
	treeCode: '',
	treeDevList: [] as any,
	treeFloorLoading: false

})

// 初始化列表数据
const getTableData = () => {
	useAccessAlarmByAreaPageApi(state.tableData.param).then(({ content }) => {
		state.tableData.data = content || []
	})
	// 数据总数（模拟，真实从接口取）

};

let timer1 = null

// 主页面获取树状菜单
// const getTreeMenus = () => {
// 	useAccessAreaTreeApi(state.tableData.param.parentCode).then(({ content }) => {
// 		treeState.treeData = content
// 		getImg(treeState.treeData[0].code)
// 	})
// }

const getImg = (code: string) => {
	treeState.treeFloorLoading = true
	useAccessAreaQueryApi(code).then(({ content }) => {
		treeState.images = content.image
		treeState.treeName = content.name
		treeState.treeCode = content.code
		treeState.treeFloorLoading = false
		getAreaDev(treeState.treeCode)
	})
}

const getAreaDev = (areaCode: string) => {
	useAccessPointByAreaQueryApi(areaCode).then(({ content }) => {
		treeState.treeDevList = content
	})
}


// 勾选关联查询table
const selectedCode = (data: any) => {
	console.log(data, '123123')
	getImg(data.code)
	state.tableData.param.areaCode = data.code
	state.tableData.param = Object.assign({}, state.tableData.param);
	getTableData()
}
// 页面加载时
onMounted(() => {
	useAccessAreaTreeApi(state.tableData.param.parentCode).then(({ content }) => {
		treeState.treeData = content
		state.tableData.param.areaCode = treeState.treeData[0].code
		getTableData();
		timer1 = setInterval(() => {
			getTableData();
		}, 1000)
		getImg(treeState.treeData[0].code)
	})
});

onUnmounted(() => {
	clearInterval(timer1)
})

</script>

<style scoped lang="scss">
.table-demo-container {
	.table-demo-padding {
		padding: 15px;

		.table-demo {
			flex: 1;
			overflow: hidden;
		}
	}
}

.menu-left {
	margin-right: 10px;
	overflow-y: auto;
	padding: 0 5px 30px 0;
	min-width: 180px;
}
</style>
